// DlgSetAccount.cpp : 实现文件
//

#include "AIPanelDataAuthority.h"

#include <uam/UserAccountManager.h>

#include <QColor>
#include <QFontMetrics>
#include <QStylePainter>

#include "../StringResource.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class CAIPanelDataAuthority
CAIPanelDataAuthority::CAIPanelDataAuthority(
    CAIPanelDataAuthority::type nType) {
  m_nType = nType;
  switch (m_nType) {
    case typeCFETS:
      m_strType = NewStringTable::strAIPanelCFETS(0);
      break;  // CFETS
    case typeSSE:
      m_strType = NewStringTable::strAIPanelSHE(0);
      break;
    case typeSZE:
      m_strType = NewStringTable::strAIPanelSZE(0);
      break;
    default:
      m_strType = "";
      break;
  }

  m_imgSuccess = ImgMgr::instance().image("guiframe_icon_tips_success");
}

CAIPanelDataAuthority::~CAIPanelDataAuthority() {}

void CAIPanelDataAuthority::Init() {
  if (m_nType <= typeNone || m_nType >= typeInvalid) return;

  m_nHeight = (24 + 8) * 2;
}

void CAIPanelDataAuthority::Draw(QPainter* painter, int top) {
  if (m_nType <= typeNone || m_nType >= typeInvalid) return;

  bool bValid = false;
  switch (m_nType) {
    case typeCFETS:
      bValid = SSUserAccountManager::instance().GetCFETS_Permission();
      break;
    case typeSSE:
      bValid = SSUserAccountManager::instance().GetSSE_Permission();
      break;
    case typeSZE:
      bValid = SSUserAccountManager::instance().GetSZE_Permission();
      break;
    default:
      assert(0);
      break;
  }

  QColor brHead = QColor(230, 179, 66);
  QColor brFront = NAME_COLOR("TextNormal");

  QString strTemp;
  switch (m_nType) {
    case typeCFETS:
      strTemp = NewStringTable::strAIPanelCFETS(1);
      break;
    case typeSSE:
      strTemp = NewStringTable::strAIPanelSHE(1);
      break;
    case typeSZE:
      strTemp = NewStringTable::strAIPanelSZE(1);
      break;
    default:
      assert(0);
      break;
  }
  int nGap = 8;
  // 数据权限-CFETS
  QRect r = rect();
  int X = nGap;
  int Y = 0;

  QRect textRect = QRect(X, Y, r.right() - X, Y + 24);
  painter->setPen(QPen(brHead));
  painter->setFont(m_lfHead);
  painter->drawText(textRect, strTemp,
                    QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
  X = X + 24;
  Y += 32;
  int nMaxWidth = 1000;
  int nImgHeight = 20;
  nImgHeight = m_imgSuccess->height();
  painter->setFont(m_lfContent);
  if (bValid) {
    if (m_imgSuccess) {
      int x = X + (24 - nImgHeight) / 2;
      int y = Y + (24 - nImgHeight) / 2;
      QPixmap m_tipssuccess = QPixmap::fromImage(*m_imgSuccess);
      painter->drawPixmap(x, y, m_tipssuccess);
      X += 24 + 1;
    }
    switch (m_nType) {
      case typeCFETS:
        strTemp = NewStringTable::strAIPanelCFETS(2);
        break;
      case typeSSE:
        strTemp = NewStringTable::strAIPanelSHE(2);
        break;
      case typeSZE:
        strTemp = NewStringTable::strAIPanelSZE(2);
        break;
      default:
        assert(0);
        break;
    }

    QRect textRect = QRect(X, Y, nMaxWidth, 24);
    painter->setPen(QPen(brFront));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
  } else {
    // 无权限
    switch (m_nType) {
      case typeCFETS:
        strTemp = NewStringTable::strAIPanelCFETS(3);
        break;
      case typeSSE:
        strTemp = NewStringTable::strAIPanelSHE(3);
        break;
      case typeSZE:
        strTemp = NewStringTable::strAIPanelSZE(3);
        break;
      default:
        assert(0);
        break;
    }
    QFontMetrics fontMetrics(m_lfContent);
    int nFontWidth = fontMetrics.width(strTemp);
    QRect textRect = QRect(X, Y, nMaxWidth, nImgHeight);
    painter->setPen(QPen(brFront));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

    X += nFontWidth;
    strTemp = NewStringTable::strAIPanelCFETS(4);  // 400-119-9898
    fontMetrics = QFontMetrics(m_lfContent);
    nFontWidth = fontMetrics.width(strTemp);
    textRect = QRect(X, Y, nMaxWidth, nImgHeight);
    painter->setPen(QPen(brHead));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

    X += nFontWidth;
    strTemp = NewStringTable::strAIPanelCFETS(5);  // 咨询权限事宜

    textRect = QRect(X, Y, nMaxWidth, nImgHeight);
    painter->setPen(QPen(brFront));
    painter->drawText(textRect, strTemp,
                      QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
  }
}

void CAIPanelDataAuthority::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);
  Draw(&painter, 0);
}
